package com.yyaccp.appinfo.controller;

import com.yyaccp.appinfo.domain.BackendUser;
import com.yyaccp.appinfo.exception.LoginException;
import com.yyaccp.appinfo.service.BackendUserService;
import com.yyaccp.appinfo.utils.AjaxResult;
import com.yyaccp.appinfo.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/adm/AdmUsers")
public class AdminUserContRoller {
    @Autowired
    private BackendUserService backendUserService;

    @PostMapping("/login")
    public AjaxResult login(BackendUser user, HttpSession session){
        try {
            BackendUser admUser = backendUserService.login(user);
            admUser.setUserPassword("******");
            session.setAttribute(Constants.LOGIN_USER,admUser);
            return AjaxResult.success(admUser);
        }catch (LoginException ex){
            return AjaxResult.error(ex.getMessage());
        }
    }

    @GetMapping("/getLoginUser")
    public AjaxResult getLoginUser(HttpSession session){
        BackendUser user = (BackendUser) session.getAttribute(Constants.LOGIN_USER);
        if (user == null) {
            return AjaxResult.error();
        }
        return AjaxResult.success(user);
    }


}
